<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
	

	
      <el-form-item label="员工名称" prop="employeeName">
        <el-input v-model="formData.employeeName" placeholder="请输入员工名称" 
        :maxlength="100"
        show-word-limit
        />
      </el-form-item>
      <el-form-item label="员工编号" prop="employeeCode">
        <el-input v-model="formData.employeeCode" placeholder="请输入员工编号" 
        :maxlength="100"
        show-word-limit
        />
      </el-form-item>
      <el-form-item label="工作经历" prop="workBackground">
        <el-input v-model="formData.workBackground" placeholder="请输入工作经历" 
        :maxlength="1024"
        show-word-limit
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="formData.sex">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
		
		<el-form-item label="年龄" prop="age">
        <el-input-number class="!w-240px"  v-model="formData.age" controls-position="right" placeholder="请输入年龄" />
      </el-form-item>
		
      <el-form-item label="兴趣爱好" prop="hobbies">
        <el-input v-model="formData.hobbies" placeholder="请输入兴趣爱好" 
        :maxlength="128"
        show-word-limit
        />
      </el-form-item>
      <el-form-item label="学历" prop="education">
        <el-input v-model="formData.education" placeholder="请输入学历" 
        :maxlength="128"
        show-word-limit
        />
      </el-form-item>
      <el-form-item label="打卡时间" prop="officeHours">
        <el-input v-model="formData.officeHours" placeholder="请输入打卡时间" 
        />
      </el-form-item>
      <el-form-item label="工作时间" prop="workingHours">
        <el-input v-model="formData.workingHours" placeholder="请输入工作时间" 
        :maxlength="128"
        show-word-limit
        />
      </el-form-item>
      <el-form-item label="出生日期" prop="birth">
        <el-input v-model="formData.birth" placeholder="请输入出生日期" 
        :maxlength="128"
        show-word-limit
        />
      </el-form-item>
      <el-form-item label="工作起止" prop="workDateFrom">
        <el-input v-model="formData.workDateFrom" placeholder="请输入工作起止" 
        :maxlength="128"
        show-word-limit
        />
      </el-form-item>
      <el-form-item label="信息保密" prop="privacy">
        <el-input v-model="formData.privacy" placeholder="请输入信息保密" 
        :maxlength="128"
        show-word-limit
        />
      </el-form-item>
		
		<el-form-item label="自评分数" prop="selfRatingScore">
        <el-input-number class="!w-240px"  v-model="formData.selfRatingScore" controls-position="right" placeholder="请输入自评分数" />
      </el-form-item>
		
      <el-form-item label="意见" prop="opinion">
        <el-input v-model="formData.opinion" placeholder="请输入意见" 
        :maxlength="65535"
        show-word-limit
        />
      </el-form-item>
      <el-form-item label="是否可上岗" prop="mountGuard">
        <el-input v-model="formData.mountGuard" placeholder="请输入是否可上岗" 
        :maxlength="128"
        show-word-limit
        />
      </el-form-item>
      <el-form-item label="完成进度" prop="slider21678">
        <el-input v-model="formData.slider21678" placeholder="请输入完成进度" 
        :maxlength="128"
        show-word-limit
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { EmpInfoApi, EmpInfoVO } from '@/api/form/empinfo/index' 






import { validateLimit } from '@/utils/formRules'

/** 审批员工信息 表单 */
defineOptions({ name: 'EmpInfoForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const tableName01 = ref('form_emp_info.')
const tableName02 = ref('form_emp_info.')

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  employeeName: undefined,
  employeeCode: undefined,
  workBackground: undefined,
  sex: undefined,
  age: undefined,
  hobbies: undefined,
  education: undefined,
  officeHours: undefined,
  workingHours: undefined,
  birth: undefined,
  workDateFrom: undefined,
  privacy: undefined,
  selfRatingScore: undefined,
  opinion: undefined,
  mountGuard: undefined,
  slider21678: undefined,
})
const formRules = reactive({
  employeeName: [
  
          { required: true, message: '员工名称不能为空', trigger: 'blur' },
         { validator: validateLimit(  null  ,  100.0  , true  ), trigger: 'blur' }

  ],
  employeeCode: [
  
          { required: true, message: '员工编号不能为空', trigger: 'blur' },
         { validator: validateLimit(  null  ,  100.0  , true  ), trigger: 'blur' }

  ],
  sex: [
  
     { required: true, message: '性别不能为空', trigger:'change' },

  ],
  age: [
  
          { required: true, message: '年龄不能为空', trigger: 'blur' },

  ],
  education: [
  
          { required: true, message: '学历不能为空', trigger: 'blur' },
         { validator: validateLimit(  null  ,  128.0  , true  ), trigger: 'blur' }

  ],
  officeHours: [
  
          { required: true, message: '打卡时间不能为空', trigger: 'blur' },

  ],
  workingHours: [
  
          { required: true, message: '工作时间不能为空', trigger: 'blur' },
         { validator: validateLimit(  null  ,  128.0  , true  ), trigger: 'blur' }

  ],
  birth: [
  
          { required: true, message: '出生日期不能为空', trigger: 'blur' },
         { validator: validateLimit(  null  ,  128.0  , true  ), trigger: 'blur' }

  ],
  workDateFrom: [
  
          { required: true, message: '工作起止不能为空', trigger: 'blur' },
         { validator: validateLimit(  null  ,  128.0  , true  ), trigger: 'blur' }

  ],
  privacy: [
  
          { required: true, message: '信息保密不能为空', trigger: 'blur' },
         { validator: validateLimit(  null  ,  128.0  , true  ), trigger: 'blur' }

  ],
  selfRatingScore: [
  
          { required: true, message: '自评分数不能为空', trigger: 'blur' },

  ],
})
const formRef = ref() // 表单 Ref


/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await EmpInfoApi.getEmpInfo(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()

  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as EmpInfoVO

    if (formType.value === 'create') {
      await EmpInfoApi.createEmpInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await EmpInfoApi.updateEmpInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  Object.keys(formData.value).forEach((key) => {
    formData.value[key] = undefined
  })
  formRef.value?.resetFields()
}


</script>